//
//  FahuoTableViewCell.m
//  customer
//
//  Created by 往返网络 on 17/4/18.
//  Copyright © 2017年 com. All rights reserved.
//

#import "FahuoTableViewCell.h"
#define begin_x 30.0/W_BASE*SCREEN_W
#define begin_x1 62.0/W_BASE*SCREEN_W

@implementation FahuoTableViewCell
{
    UILabel * doculab;
    
    UILabel * statuslab;  //当前运单状态
    
    UILabel * iconlab; // 表明是发货cell，还是收货cell
    
    UILabel * namelab;
    
    UILabel * companylab;
    
    UILabel * phonelab;
    
    UIButton * phonebtn;  //拨打电话
    
    UILabel * timelab;
    
    UILabel * yunfeilab;
    
    UILabel * goodslab;
    
    UIButton * goodsfollowbtn;  //货物跟踪
    
    NSString * phonestring; //电话
    
    NSString * current_id; //运单id
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self createUI];
        
    }

    return self;
    
}

- (void)createUI{

    float fontsize = 13;
    
    fontsize = SCREEN_H < 667 ? 11 : 15;
    
    if (SCREEN_H == 667) {
        
        fontsize = 13;
    }
    
    self.backgroundColor = [UIColor whiteColor];
    
    doculab = [[UILabel alloc]initWithFrame:CGRectMake(begin_x, 0, 471.0/W_BASE*SCREEN_W, 85.0/H_BASE*SCREEN_H)];
    
    
    doculab.font = [UIFont boldSystemFontOfSize:fontsize+1];
    
    [self.contentView addSubview:doculab];
    
    statuslab = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_W - begin_x - 110.0/W_BASE*SCREEN_W , 0, 110.0/W_BASE*SCREEN_W, CGRectGetHeight(doculab.frame))];
    
    
    statuslab.textAlignment = NSTextAlignmentRight;
    
    statuslab.font = [UIFont boldSystemFontOfSize:fontsize];
    
    statuslab.textColor = COLORRGBA(38, 146, 255, 1);
    
    [self.contentView addSubview:statuslab];
    
    UIView * breakline = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(doculab.frame), SCREEN_W, 1)];
    
    breakline.backgroundColor = COLORRGBA(239, 239, 239, 1);
    
    [self.contentView addSubview:breakline];
    
    iconlab = [[UILabel alloc]initWithFrame:CGRectMake(begin_x, CGRectGetMaxY(breakline.frame)+21.0/H_BASE*SCREEN_H, 34.0/W_BASE*SCREEN_W, 44.0/H_BASE*SCREEN_H)];
    
    iconlab.textColor = COLORRGBA(255, 102, 102, 1);
    
    [self.contentView addSubview:iconlab];
    
    iconlab.font = [UIFont fontWithName:@"iconfont" size:fontsize+5];
    
    iconlab.text = @"\U0000e663";
    
    namelab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(iconlab.frame)+5.0/W_BASE*SCREEN_W, CGRectGetMinY(iconlab.frame), 195.0/W_BASE*SCREEN_W, CGRectGetHeight(iconlab.frame))];
    
    
    [self.contentView addSubview:namelab];
    
    namelab.font = [UIFont boldSystemFontOfSize:fontsize];
    
    
    
    phonelab = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_W - begin_x - 348.5/W_BASE*SCREEN_W , CGRectGetMinY(namelab.frame), 311.0/W_BASE*SCREEN_W, CGRectGetHeight(namelab.frame))];
    
    
    phonelab.textAlignment  = NSTextAlignmentRight;
    
    phonelab.font = [UIFont systemFontOfSize:fontsize];
    
    [self.contentView addSubview:phonelab];
    
    UIButton * bigbtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_W - begin_x - 348.5/W_BASE*SCREEN_W , CGRectGetMinY(namelab.frame), 311.0/W_BASE*SCREEN_W, CGRectGetHeight(namelab.frame))];
    
    [bigbtn addTarget:self action:@selector(tapgest) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:bigbtn];
    
    phonebtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_W - begin_x -  33.5/W_BASE*SCREEN_W, CGRectGetMinY(phonelab.frame), 33.5/W_BASE*SCREEN_W, CGRectGetHeight(phonelab.frame))];
    
    phonebtn.titleLabel.font  = [UIFont fontWithName:@"iconfont" size:fontsize+5];
    
    [phonebtn setTitleEdgeInsets:UIEdgeInsetsMake(30.0/H_BASE*SCREEN_H, 0, 10, 0)];
    
    
    [phonebtn setTitle:@"\U0000e662" forState:UIControlStateNormal];
    
    [phonebtn setTitleColor:COLORRGBA(153, 153, 153, 1) forState:UIControlStateNormal];
    
    
    [self.contentView addSubview:phonebtn];
    
    companylab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(namelab.frame), CGRectGetMaxY(namelab.frame), 340.0/W_BASE*SCREEN_W, CGRectGetHeight(namelab.frame))];
    
    
    companylab.font = [UIFont systemFontOfSize:fontsize];
    
    [self.contentView addSubview:companylab];
    
    timelab = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_W - CGRectGetWidth(phonebtn.frame)-186.0/W_BASE*SCREEN_W - begin_x, CGRectGetMinY(companylab.frame), 186.0/W_BASE*SCREEN_W, CGRectGetHeight(companylab.frame))];
    
    timelab.font = [UIFont systemFontOfSize:fontsize];
    
    timelab.text = @"12/30 10:00";
    
    timelab.textAlignment = NSTextAlignmentRight;
    
    [self.contentView addSubview:timelab];
    
    
    UIView * breakline2 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(companylab.frame)+21.0/H_BASE*SCREEN_H, SCREEN_W, 1)];
    
    breakline2.backgroundColor = COLORRGBA(239, 239, 239, 1);
    
    [self.contentView addSubview:breakline2];
    
    yunfeilab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(namelab.frame), CGRectGetMaxY(breakline2.frame)+21.0/H_BASE*SCREEN_H, CGRectGetWidth(companylab.frame), CGRectGetHeight(companylab.frame))];
    
    
    
    yunfeilab.font = [UIFont systemFontOfSize:fontsize];
    
    [self.contentView addSubview:yunfeilab];
    
    goodslab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(namelab.frame), CGRectGetMaxY(yunfeilab.frame), CGRectGetWidth(yunfeilab.frame)+125.0/W_BASE*SCREEN_W, CGRectGetHeight(yunfeilab.frame))];
    
    goodslab.font = [UIFont systemFontOfSize:fontsize];
    
    
    
    [self.contentView addSubview:goodslab];
    
    goodsfollowbtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_W - begin_x - 150.0/W_BASE*SCREEN_W, CGRectGetMaxY(breakline2.frame)+35.0/H_BASE*SCREEN_H, 150.0/W_BASE*SCREEN_W, 60.0/H_BASE*SCREEN_H)];
    goodsfollowbtn.backgroundColor = COLORRGBA(51, 204, 102, 1);
    
    [self.contentView addSubview:goodsfollowbtn];
    
    [goodsfollowbtn setTitle:@"货物跟踪" forState:UIControlStateNormal];
    
    goodsfollowbtn.titleLabel.font = [UIFont systemFontOfSize:fontsize];
    
    [goodsfollowbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    goodsfollowbtn.layer.cornerRadius = 2;
    
    goodsfollowbtn.clipsToBounds = YES;
    
    UIView * bottomview = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(goodslab.frame)+21.0/H_BASE*SCREEN_H, SCREEN_W, 20.0/H_BASE*SCREEN_H)];
    
    bottomview.backgroundColor = COLORRGBA(239, 239, 239, 1);
    
    [self.contentView addSubview:bottomview];
    
    
    phonebtn.tag = 1000;
    
    goodsfollowbtn.tag = 2000;
    
    [phonebtn addTarget:self action:@selector(enterClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [goodsfollowbtn addTarget:self action:@selector(enterClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
}


//创建一个返回富文本的方法

-(NSMutableAttributedString*) changeLabelWithText:(NSString*)needText andStartplace:(NSString *)startplace andendplae:(NSString *)endplace andfontsize:(float)fontsize
{
    
    //
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:needText];
    
    NSInteger startlength = startplace.length;
    
    NSInteger endlength = endplace.length;
    
     //NSForegroundColorAttributeName
    
    [attrString addAttribute:NSForegroundColorAttributeName value:COLORRGBA(204, 204, 204, 1) range:NSMakeRange(0,startlength)];
    
    [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(startlength , endlength)];
    
    
    
    return attrString;
    
    
    
}

#pragma mark  -  -  货物明细text格式化 － － －-- - -  -

-(NSMutableAttributedString*) changeLabelWithText2:(NSString*)needText andStartplace:(NSString *)startplace andendplae:(NSString *)endplace andfontsize:(float)fontsize
{
    
    //
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:needText];
    
    NSInteger startlength = startplace.length;
    
    NSInteger endlength = endplace.length;
    
     
    //起始格式化
    [attrString addAttribute:NSForegroundColorAttributeName value:COLORRGBA(204, 204, 204, 1) range:NSMakeRange(0,startlength)];
    
    //中间格式化
    [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(startlength , needText.length - startlength - endlength)];
    
      //尾部格式化
    
    [attrString addAttribute:NSForegroundColorAttributeName value:COLORRGBA(204, 204, 204, 1) range:NSMakeRange(needText.length  - endlength , endlength)];

    
    return attrString;
    
    
    
}




#pragma mark  - - --  CELL 赋值 - - - - --  -- - 

-(void)refreshCellWithModel:(SendListModel *)model{
    
    
    //设置为 收货列表页的icon

    if (!self.isfahuo) {
        
      iconlab.text = @"\U0000e664";

    }
    
    float fontsize = 13;
    
    fontsize = SCREEN_H < 667 ? 11 : 15;
    
    if (SCREEN_H == 667) {
        
        fontsize = 13;
    }

    NSArray * statusarr = @[@"未配载",@"已配载",@"已发车",@"已到达",@"已完成"];
    
    doculab.text = [NSString stringWithFormat:@"运单号: %@",model.order_id];
    
    NSInteger num = [model.order_state integerValue];
    
    NSString * status = statusarr[num - 1];
    
    statuslab.text = [NSString stringWithFormat:@"[%@]",status];
    
    if ([status isEqualToString:@"已配载"] ||[status isEqualToString:@"已完成"]) {
        
        statuslab.textColor = COLORRGBA(38, 146, 255, 1);

    }else{
    
        statuslab.textColor = COLORRGBA(255, 102, 102, 1);

        
    }
    
    namelab.text = [NSString stringWithFormat:@"姓名: %@",model.to_person];

    namelab.attributedText = [self changeLabelWithText:namelab.text andStartplace:@"姓名: " andendplae: model.to_person andfontsize:fontsize];
    
    phonelab.text = [NSString stringWithFormat:@"电话: %@",model.to_mobile];
    
    
    phonelab.attributedText = [self changeLabelWithText:phonelab.text andStartplace:@"电话: " andendplae:model.to_mobile andfontsize:fontsize];
    
    companylab.text = [NSString stringWithFormat:@"公司: %@",model.company_name];

    companylab.attributedText = [self changeLabelWithText:companylab.text andStartplace:@"公司: " andendplae:model.company_name  andfontsize:fontsize];
    
    timelab.text = [NSString stringWithFormat:@"%@",model.created_time];
    
    NSString * feestr = [NSString stringWithFormat:@"%@元",model.fee];

    yunfeilab.text = [NSString stringWithFormat:@"运费: %@",feestr];
    
//    NSLog(@"feestr %@",model.fee);
    yunfeilab.attributedText = [self changeLabelWithText:yunfeilab.text andStartplace:@"运费: " andendplae:feestr andfontsize:fontsize];
    
    //如果没有数量，则不显示单位
    
    NSString * weigtstr = [NSString stringWithFormat:@"%@吨 ",model.goods_weight];
    
    NSString * volustr = [NSString stringWithFormat:@"%@方 ",model.goods_volume];
    
    NSString * numstr = [NSString stringWithFormat:@"%@件 ",model.goods_num];

    if ([model.goods_num isEqualToString:@""]) {
        
    
        numstr = @"";
        
    }
    
    if ([model.goods_volume isEqualToString:@""]) {
        
        volustr = @"";
        
    }
    
    if ([model.goods_weight isEqualToString:@""]) {
        
        weigtstr = @"";
    }
    

    goodslab.text = [NSString stringWithFormat:@"货物: %@ (%@%@%@)",model.goods_name,weigtstr,volustr,numstr];
    
    NSString * goodsend = [NSString stringWithFormat:@"(%@%@%@)",weigtstr,volustr,numstr];

    
    goodslab.attributedText = [self changeLabelWithText2:goodslab.text andStartplace:@"货物: " andendplae:goodsend andfontsize:fontsize];

    //如果数量，件数都没有，则()也不显示
    
    if ([model.goods_num isEqualToString:@""] && [model.goods_volume isEqualToString:@""]&&[model.goods_weight isEqualToString:@""]) {
        
        goodslab.text = [NSString stringWithFormat:@"货物: %@",model.goods_name];
        
        goodslab.attributedText = [self changeLabelWithText2:goodslab.text andStartplace:@"货物: " andendplae:@"" andfontsize:fontsize];
        
        
    }

    current_id = model.id;
    
    
    phonestring = model.to_mobile;
    
}

#pragma mark  -- - - - - - -- - 代理回调－ － － － －－ － － － －

- (void)enterClick:(UIButton *)btn{

    if (btn.tag ==  1000) {
        
        //打电话
        self.block1(phonestring);
        
        
    }else{
        
        //运单跟踪
    
        self.block2(current_id);
        
                
    }

}
- (void)tapgest{
    
    //打电话

    self.block1(phonestring);
    


}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
